package com.example.siyun.controller.carouselcontroller;

import com.example.siyun.controller.ordercontroller.OrderController;
import com.example.siyun.pojo.Carousel;
import com.example.siyun.service.carouselservice.CarouselService;
import com.example.siyun.util.PageResult;
import com.example.siyun.util.WebUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author XZZ
 * @Date 2022/9/14 10：50（加注解时间）
 * @Decription :广告轮播图的控制层
 */
@RestController
@RequestMapping("/manager/advertiding")
@Slf4j
public class CarouselController {


    @Autowired
    private CarouselService carouselService;

    /**
     *
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping("/getAll")
    public PageResult<Carousel> getAll(@RequestParam(defaultValue = "10")Integer limit,
                             @RequestParam(defaultValue = "10")Integer page){
        PageHelper.startPage(page,limit);
        List<Carousel> all = carouselService.getAll();
        PageInfo<Carousel> info = new PageInfo<>(all);
        PageResult js = new PageResult();
        js.setData(info.getList());
        js.setMessage("成功");
        js.setTotal(info.getTotal());
        System.out.println(js.getTotal());
        js.setState(0);
        return js;
    }

    /**
     *  表格数据删除 根据数据编号
     * @param number
     * @return
     */
    @RequestMapping("/del")
public PageResult<Carousel> del(Integer number){
        log.info("根据num删除数据[开始删除],number:{}",number);
    carouselService.del(number);
    PageResult<Carousel> js = new PageResult();
    js.setMessage("成功");
    log.info("根据num删除数据[删除结束],number:{},msg:{}",number,js.getMessage());
    return js;
}

    /**
     * 表格数据修改
     * @param carousel
     * @return
     */
    @RequestMapping("/update")
    public PageResult<Carousel> update(Carousel carousel){
        log.info("修改数据[开始]，数据对象 Carousel:{}",carousel);
        carouselService.update(carousel);
        PageResult<Carousel> js = new PageResult();
        js.setMessage("成功");
        log.info("修改数据[结束]，数据对象 Carousel:{},msg:{}",carousel,js.getMessage());
        return js;
    }

    /**
     * 模糊查询
     * @param imageType
     * @param isOnline
     * @return
     */
    @RequestMapping("/getCaByType")
    public PageResult<Carousel> getCaByType(String imageType, String isOnline){
        List<Carousel> caByTitle = carouselService.getCaByTitle(imageType, isOnline);
        PageResult js = new PageResult();
        js.setData(caByTitle);
        js.setState(0);
        return js;
    }

    /**
     * 广澳数据添加
     * @param carousel
     * @return
     */
    @RequestMapping("/insert")
    public PageResult<Carousel> insert(Carousel carousel){
        String username = (String) WebUtils.getSession().getAttribute("username");
        carousel.setUserNum(username);
        carouselService.insert(carousel);
        PageResult<Carousel> js = new PageResult();
        js.setMessage("成功");
        js.setState(200);
        return js;
    }

}
